#include "../mq_server/mq_binding.hpp"
#include <gtest/gtest.h>

using namespace mq_server;

BindingManager::ptr bmp;

class ExchangeTest : public testing::Environment {
    public:
        virtual void SetUp() override {
            bmp = std::make_shared<BindingManager>("./data/meta.db");
        }
        virtual void TearDown() override {
            // bmp->Clear();
            std::cout << "最后的清理！！\n";
        }
};

TEST(binding_test, insert_test) {
    bmp->Bind("exchange1", "queue1", "new.music.#", true);
    bmp->Bind("exchange2", "queue2", "new.sport.#", true);
    bmp->Bind("exchange3", "queue3", "new.music.pop", true);
    bmp->Bind("exchange4", "queue4", "new.sport.football", true);
    ASSERT_EQ(bmp->Size(), 4); 
}

TEST(queue_test, select_test) {
    ASSERT_EQ(bmp->Exists("exchange1", "queue1"), true);
    ASSERT_EQ(bmp->Exists("exchange2", "queue2"), true);
    ASSERT_EQ(bmp->Exists("exchange3", "queue3"), true);
    ASSERT_EQ(bmp->Exists("exchange4", "queue4"), true);

    Binding::ptr bp = bmp->GetBinding("exchange1", "queue1");
    ASSERT_NE(bp.get(), nullptr);
    ASSERT_EQ(bp->_exchange_name, std::string("exchange1"));
    ASSERT_EQ(bp->_msgqueue_name, std::string("queue1"));
    ASSERT_EQ(bp->_binding_key, std::string("new.music.#"));    
}

TEST(queue_test2, select_exchange_test) {
    MsgQueueBindingMap mqbm = bmp->GetExchangeBindings("exchange1");
    ASSERT_EQ(mqbm.size(), 3);
    ASSERT_NE(mqbm.find("queue1"), mqbm.end());
    ASSERT_NE(mqbm.find("queue2"), mqbm.end());
    ASSERT_NE(mqbm.find("queue3"), mqbm.end());
}

int main(int argc,char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    RUN_ALL_TESTS();
    return 0;
}